{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "name": "Chatting_to_users.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.11"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "7MP6Si4U_74a",
        "outputId": "fccee37f-e58e-4802-9245-c27fec732cfa",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        }
      },
      "source": [
        "!pip install git+https://www.github.com/farizrahman4u/eywa.git"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Collecting git+https://www.github.com/farizrahman4u/eywa.git\n",
            "  Cloning https://www.github.com/farizrahman4u/eywa.git to /tmp/pip-req-build-5p2u62go\n",
            "  Running command git clone -q https://www.github.com/farizrahman4u/eywa.git /tmp/pip-req-build-5p2u62go\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from eywa==0.0.4) (1.18.5)\n",
            "Collecting dateparser\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/c1/d5/5a2e51bc0058f66b54669735f739d27afc3eb453ab00520623c7ab168e22/dateparser-0.7.6-py2.py3-none-any.whl (362kB)\n",
            "\u001b[K     |████████████████████████████████| 368kB 5.7MB/s \n",
            "\u001b[?25hRequirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages (from eywa==0.0.4) (2.23.0)\n",
            "Requirement already satisfied: scipy in /usr/local/lib/python3.6/dist-packages (from eywa==0.0.4) (1.4.1)\n",
            "Collecting annoy\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/a1/5b/1c22129f608b3f438713b91cd880dc681d747a860afe3e8e0af86e921942/annoy-1.17.0.tar.gz (646kB)\n",
            "\u001b[K     |████████████████████████████████| 655kB 21.7MB/s \n",
            "\u001b[?25hCollecting responder\n",
            "  Downloading https://files.pythonhosted.org/packages/36/b9/99831331a9d22f79682f31d75b8be454c3e2781c2cca8d14e5882acf968a/responder-2.0.5-py3-none-any.whl\n",
            "Requirement already satisfied: python-dateutil in /usr/local/lib/python3.6/dist-packages (from dateparser->eywa==0.0.4) (2.8.1)\n",
            "Requirement already satisfied: pytz in /usr/local/lib/python3.6/dist-packages (from dateparser->eywa==0.0.4) (2018.9)\n",
            "Requirement already satisfied: tzlocal in /usr/local/lib/python3.6/dist-packages (from dateparser->eywa==0.0.4) (1.5.1)\n",
            "Requirement already satisfied: regex!=2019.02.19 in /usr/local/lib/python3.6/dist-packages (from dateparser->eywa==0.0.4) (2019.12.20)\n",
            "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests->eywa==0.0.4) (2.10)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests->eywa==0.0.4) (2020.6.20)\n",
            "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests->eywa==0.0.4) (1.24.3)\n",
            "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests->eywa==0.0.4) (3.0.4)\n",
            "Requirement already satisfied: docopt in /usr/local/lib/python3.6/dist-packages (from responder->eywa==0.0.4) (0.6.2)\n",
            "Collecting starlette==0.12.*\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/a4/bd/b36fae5877bd5eca9dc72434273c11c9ba8a47b8fdfe9159ebc355d26500/starlette-0.12.13.tar.gz (47kB)\n",
            "\u001b[K     |████████████████████████████████| 51kB 6.1MB/s \n",
            "\u001b[?25hCollecting whitenoise\n",
            "  Downloading https://files.pythonhosted.org/packages/50/83/5d91949e370e52578a99ef6391c3b3e19f9fd1f5b4f58d5cbd6e2862d4a8/whitenoise-5.2.0-py2.py3-none-any.whl\n",
            "Requirement already satisfied: pyyaml in /usr/local/lib/python3.6/dist-packages (from responder->eywa==0.0.4) (3.13)\n",
            "Collecting graphql-server-core>=1.1\n",
            "  Downloading https://files.pythonhosted.org/packages/2d/c4/911e0c61640a84b6f4929c854c6a16701a61bfc87e9af02ef17de4d699d7/graphql-server-core-2.0.0.tar.gz\n",
            "Collecting marshmallow\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/6a/f8/516495245005449e9493972571d1d83c37e01bd853840887617e671a30ea/marshmallow-3.8.0-py2.py3-none-any.whl (46kB)\n",
            "\u001b[K     |████████████████████████████████| 51kB 7.7MB/s \n",
            "\u001b[?25hCollecting requests-toolbelt\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/60/ef/7681134338fc097acef8d9b2f8abe0458e4d87559c689a8c306d0957ece5/requests_toolbelt-0.9.1-py2.py3-none-any.whl (54kB)\n",
            "\u001b[K     |████████████████████████████████| 61kB 8.3MB/s \n",
            "\u001b[?25hCollecting rfc3986\n",
            "  Downloading https://files.pythonhosted.org/packages/78/be/7b8b99fd74ff5684225f50dd0e865393d2265656ef3b4ba9eaaaffe622b8/rfc3986-1.4.0-py2.py3-none-any.whl\n",
            "Collecting apispec>=1.0.0b1\n",
            "  Downloading https://files.pythonhosted.org/packages/17/57/45bfcbe3c406597164983b8d383f44716aafcd15dd79eba3a0355fbb1b24/apispec-4.0.0-py2.py3-none-any.whl\n",
            "Collecting aiofiles\n",
            "  Downloading https://files.pythonhosted.org/packages/f4/2b/078a9771ae4b67e36b0c2a973df845260833a4eb088b81c84b738509b4c4/aiofiles-0.5.0-py3-none-any.whl\n",
            "Requirement already satisfied: jinja2 in /usr/local/lib/python3.6/dist-packages (from responder->eywa==0.0.4) (2.11.2)\n",
            "Collecting apistar\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/d6/0c/3066b856f661bc58b16c1a2ff4eeed25dc4f0d4618871b3454066dad89e0/apistar-0.7.2.tar.gz (3.3MB)\n",
            "\u001b[K     |████████████████████████████████| 3.3MB 32.8MB/s \n",
            "\u001b[?25hCollecting uvicorn==0.10.*\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/45/d6/8d38b80e1f1d30ef0c2a44b1a6a9ea257cd9afba6abb32100e8cf3783638/uvicorn-0.10.9-py3-none-any.whl (42kB)\n",
            "\u001b[K     |████████████████████████████████| 51kB 8.2MB/s \n",
            "\u001b[?25hRequirement already satisfied: itsdangerous in /usr/local/lib/python3.6/dist-packages (from responder->eywa==0.0.4) (1.1.0)\n",
            "Collecting python-multipart\n",
            "  Downloading https://files.pythonhosted.org/packages/46/40/a933ac570bf7aad12a298fc53458115cc74053474a72fbb8201d7dc06d3d/python-multipart-0.0.5.tar.gz\n",
            "Collecting graphene<3.0\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/05/97/45e743b372f65a619f8d1eb2897efb74fb1b0ffddc731ad37e0aa187ec5c/graphene-2.1.8-py2.py3-none-any.whl (107kB)\n",
            "\u001b[K     |████████████████████████████████| 112kB 29.3MB/s \n",
            "\u001b[?25hRequirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil->dateparser->eywa==0.0.4) (1.15.0)\n",
            "Collecting graphql-core<3,>=2.3\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/11/71/d51beba3d8986fa6d8670ec7bcba989ad6e852d5ae99d95633e5dacc53e7/graphql_core-2.3.2-py2.py3-none-any.whl (252kB)\n",
            "\u001b[K     |████████████████████████████████| 256kB 42.0MB/s \n",
            "\u001b[?25hRequirement already satisfied: promise<3,>=2.3 in /usr/local/lib/python3.6/dist-packages (from graphql-server-core>=1.1->responder->eywa==0.0.4) (2.3)\n",
            "Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.6/dist-packages (from jinja2->responder->eywa==0.0.4) (1.1.1)\n",
            "Requirement already satisfied: click in /usr/local/lib/python3.6/dist-packages (from apistar->responder->eywa==0.0.4) (7.1.2)\n",
            "Collecting typesystem\n",
            "  Downloading https://files.pythonhosted.org/packages/bd/be/158f4a02dde348e16cec7ab9603df521d9175d37ff7d048f086d9b76eb7b/typesystem-0.2.4.tar.gz\n",
            "Collecting h11==0.9.*\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/5a/fd/3dad730b0f95e78aeeb742f96fa7bbecbdd56a58e405d3da440d5bfb90c6/h11-0.9.0-py2.py3-none-any.whl (53kB)\n",
            "\u001b[K     |████████████████████████████████| 61kB 9.4MB/s \n",
            "\u001b[?25hCollecting uvloop>=0.14.0; sys_platform != \"win32\" and sys_platform != \"cygwin\" and platform_python_implementation != \"pypy\"\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/41/48/586225bbb02d3bdca475b17e4be5ce5b3f09da2d6979f359916c1592a687/uvloop-0.14.0-cp36-cp36m-manylinux2010_x86_64.whl (3.9MB)\n",
            "\u001b[K     |████████████████████████████████| 3.9MB 55.2MB/s \n",
            "\u001b[?25hCollecting httptools==0.0.13; sys_platform != \"win32\" and sys_platform != \"cygwin\" and platform_python_implementation != \"pypy\"\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/1b/03/215969db11abe8741e9c266a4cbe803a372bd86dd35fa0084c4df6d4bd00/httptools-0.0.13.tar.gz (104kB)\n",
            "\u001b[K     |████████████████████████████████| 112kB 61.1MB/s \n",
            "\u001b[?25hCollecting websockets==8.*\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/bb/d9/856af84843912e2853b1b6e898ac8b802989fcf9ecf8e8445a1da263bf3b/websockets-8.1-cp36-cp36m-manylinux2010_x86_64.whl (78kB)\n",
            "\u001b[K     |████████████████████████████████| 81kB 12.4MB/s \n",
            "\u001b[?25hCollecting aniso8601<=7,>=3\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/45/a4/b4fcadbdab46c2ec2d2f6f8b4ab3f64fd0040789ac7f065eba82119cd602/aniso8601-7.0.0-py2.py3-none-any.whl (42kB)\n",
            "\u001b[K     |████████████████████████████████| 51kB 9.9MB/s \n",
            "\u001b[?25hCollecting graphql-relay<3,>=2\n",
            "  Downloading https://files.pythonhosted.org/packages/94/48/6022ea2e89cb936c3b933a0409c6e29bf8a68c050fe87d97f98aff6e5e9e/graphql_relay-2.0.1-py3-none-any.whl\n",
            "Collecting rx<2,>=1.6\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/33/0f/5ef4ac78e2a538cc1b054eb86285fe0bf7a5dbaeaac2c584757c300515e2/Rx-1.6.1-py2.py3-none-any.whl (179kB)\n",
            "\u001b[K     |████████████████████████████████| 184kB 59.2MB/s \n",
            "\u001b[?25hBuilding wheels for collected packages: eywa, annoy, starlette, graphql-server-core, apistar, python-multipart, typesystem, httptools\n",
            "  Building wheel for eywa (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for eywa: filename=eywa-0.0.4-cp36-none-any.whl size=152474 sha256=5e7e5536e3d5706832e55681051ad93aad4d636d0ac6ab84957c0eb208202bf2\n",
            "  Stored in directory: /tmp/pip-ephem-wheel-cache-25lny_ly/wheels/e3/d3/11/d2c9b9b41cb0f2b8397d58387cc4c9ce92956c5a16fcb08c02\n",
            "  Building wheel for annoy (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for annoy: filename=annoy-1.17.0-cp36-cp36m-linux_x86_64.whl size=390355 sha256=9d307bfa42def189c87ba081291de398465cd20b4a9b8bd9ac3fb8dbf2648207\n",
            "  Stored in directory: /root/.cache/pip/wheels/3a/c5/59/cce7e67b52c8e987389e53f917b6bb2a9d904a03246fadcb1e\n",
            "  Building wheel for starlette (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for starlette: filename=starlette-0.12.13-cp36-none-any.whl size=58383 sha256=67d11e9e0501f5c7991392c63c200a1734746e7e60b5bdaad9ad3fe47e231750\n",
            "  Stored in directory: /root/.cache/pip/wheels/08/be/e8/2a06d0515b4730f45415eb97bf1b9ebbc4a22c67b4f5113885\n",
            "  Building wheel for graphql-server-core (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for graphql-server-core: filename=graphql_server_core-2.0.0-py2.py3-none-any.whl size=7728 sha256=c2c3863265cf0838f1a8c31242fabb2e77b9a9a77511497b6e8f41408cb19e59\n",
            "  Stored in directory: /root/.cache/pip/wheels/0c/81/82/c8d678001af54abb231abc5c521cb86ad7c7176bab5aff8673\n",
            "  Building wheel for apistar (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for apistar: filename=apistar-0.7.2-cp36-none-any.whl size=3312662 sha256=cd0d091d14110dbf9c02ee294332de9f9b1075f4ef37817819a97d440541c911\n",
            "  Stored in directory: /root/.cache/pip/wheels/32/b5/a5/49563b6328da6f18de11d34c9167b5ff036808b9bf2e2941b3\n",
            "  Building wheel for python-multipart (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for python-multipart: filename=python_multipart-0.0.5-cp36-none-any.whl size=31671 sha256=1b2f621ed4ce76812c11e62712c1fde6a82645f5c38c684fa8671e0ae6abb7d1\n",
            "  Stored in directory: /root/.cache/pip/wheels/f0/e6/66/14a866a3cbd6a0cabfbef91f7edf40aa03595ef6c88d6d1be4\n",
            "  Building wheel for typesystem (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for typesystem: filename=typesystem-0.2.4-cp36-none-any.whl size=26039 sha256=0d345b5a7f935078b687530190d6ca238e56a3deb64050c4e2c6305f0e264ff8\n",
            "  Stored in directory: /root/.cache/pip/wheels/25/05/bf/d42f7a013cc83b042eb546b5749108d8a664bac99f44dacc9e\n",
            "  Building wheel for httptools (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for httptools: filename=httptools-0.0.13-cp36-cp36m-linux_x86_64.whl size=212529 sha256=0234bf32b54033eb1776e7c977d6a1d5491a1ec941c719532c926c7bfe372856\n",
            "  Stored in directory: /root/.cache/pip/wheels/e8/3e/2e/013f99b42efc25cf3589730cf380738e46b1e5edaf2f78d525\n",
            "Successfully built eywa annoy starlette graphql-server-core apistar python-multipart typesystem httptools\n",
            "Installing collected packages: dateparser, annoy, starlette, whitenoise, rx, graphql-core, graphql-server-core, marshmallow, requests-toolbelt, rfc3986, apispec, aiofiles, typesystem, apistar, h11, uvloop, httptools, websockets, uvicorn, python-multipart, aniso8601, graphql-relay, graphene, responder, eywa\n",
            "Successfully installed aiofiles-0.5.0 aniso8601-7.0.0 annoy-1.17.0 apispec-4.0.0 apistar-0.7.2 dateparser-0.7.6 eywa-0.0.4 graphene-2.1.8 graphql-core-2.3.2 graphql-relay-2.0.1 graphql-server-core-2.0.0 h11-0.9.0 httptools-0.0.13 marshmallow-3.8.0 python-multipart-0.0.5 requests-toolbelt-0.9.1 responder-2.0.5 rfc3986-1.4.0 rx-1.6.1 starlette-0.12.13 typesystem-0.2.4 uvicorn-0.10.9 uvloop-0.14.0 websockets-8.1 whitenoise-5.2.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "g0rnWtvyJPK6",
        "outputId": "318b9ad3-37f5-4692-8438-f5f8c5466ab8",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 241
        }
      },
      "source": [
        "!pip install pyowm"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Collecting pyowm\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/41/2a/83e26bc87763d0d34767ddc5c875608d4a0a0da66e59730a15c55aec6eff/pyowm-2.10.0-py3-none-any.whl (3.7MB)\n",
            "\u001b[K     |████████████████████████████████| 3.8MB 4.4MB/s \n",
            "\u001b[?25hRequirement already satisfied: requests<3,>=2.20.0 in /usr/local/lib/python3.6/dist-packages (from pyowm) (2.23.0)\n",
            "Collecting geojson<3,>=2.3.0\n",
            "  Downloading https://files.pythonhosted.org/packages/e4/8d/9e28e9af95739e6d2d2f8d4bef0b3432da40b7c3588fbad4298c1be09e48/geojson-2.5.0-py2.py3-none-any.whl\n",
            "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.20.0->pyowm) (2.10)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.20.0->pyowm) (2020.6.20)\n",
            "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.20.0->pyowm) (1.24.3)\n",
            "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.20.0->pyowm) (3.0.4)\n",
            "Installing collected packages: geojson, pyowm\n",
            "Successfully installed geojson-2.5.0 pyowm-2.10.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "U37DdYnTa5DO",
        "outputId": "2ffe4910-d3f4-4702-fe82-1acd1cd7a168",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 340
        }
      },
      "source": [
        "from eywa.nlu import Classifier\n",
        "\n",
        "CONV_SAMPLES = {\n",
        "    'greetings' : ['Hi', 'hello', 'How are you', 'hey there', 'hey'],\n",
        "    'taxi'      : ['book a cab', 'need a ride', 'find me a cab'],\n",
        "    'weather'   : ['what is the weather in tokyo', 'weather germany',\n",
        "                   'what is the weather like in kochi',\n",
        "                   'what is the weather like', 'is it hot outside'],\n",
        "    'datetime'  : ['what day is today', 'todays date', 'what time is it now',\n",
        "                   'time now', 'what is the time'],\n",
        "    'music'     : ['play the Beatles', 'shuffle songs', 'make a sound']\n",
        "}\n",
        "\n",
        "CLF = Classifier()\n",
        "for key in CONV_SAMPLES:\n",
        "    CLF.fit(CONV_SAMPLES[key], key)\n",
        "\n",
        "print(CLF.predict('will it rain today'))  # >>> 'weather'\n",
        "print(CLF.predict('play playlist rock n\\'roll'))  # >>> 'music'\n",
        "print(CLF.predict('what\\'s the hour?'))  # >>> 'datetime'"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Downloading embeddings...\n",
            "Source: https://github.com/explosion/sense2vec/releases/download/v1.0.0a0/reddit_vectors-1.1.0.tar.gz\n",
            "Destination /root/.eywa/lang/en/embeddings/reddit_vectors-1.1.0.tar.gz\n",
            "Size: 560.460747718811MB\n",
            "586899456/587685689 [============================>.] - 99.866215% - ETA: 0sDone.\n",
            "Extracting...\n",
            "Done.\n",
            "Converting embeddings...\n",
            "Done.\n",
            "Seems you are running the program for the first time. Building index...\n",
            "Building tree...\n",
            "1193838/1195260 [============================>.] - 99.881030% - ETA: 0sDone.\n",
            "Creating databases...\n",
            "1194090/1195260 [============================>.] - 99.902113% - ETA: 0s\n",
            "Converting words to indices...\n",
            "1191011/1195260 [============================>.] - 99.644512% - ETA: 0sDone.\n",
            "datetime\n",
            "music\n",
            "weather\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KBS524RIGk_h"
      },
      "source": [
        "from eywa.nlu import EntityExtractor\n",
        "\n",
        "X_WEATHER = ['what is the weather in tokyo', 'weather germany', 'what is the weather like in kochi']\n",
        "Y_WEATHER = [{'intent': 'weather', 'place': 'tokyo'}, {'intent': 'weather', 'place': 'germany'},\n",
        "             {'intent': 'weather', 'place': 'kochi'}]\n",
        "\n",
        "EX_WEATHER = EntityExtractor()\n",
        "EX_WEATHER.fit(X_WEATHER, Y_WEATHER)\n"
      ],
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Cj70xxYjGsr9",
        "outputId": "e302580e-880c-4f52-d609-1960d154c8fb",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 156
        }
      },
      "source": [
        "EX_WEATHER.predict('what is the weather in London')"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "WARNING:tensorflow:Layer gru will not use cuDNN kernel since it doesn't meet the cuDNN kernel criteria. It will use generic GPU kernel as fallback when running on GPU\n",
            "WARNING:tensorflow:Layer gru will not use cuDNN kernel since it doesn't meet the cuDNN kernel criteria. It will use generic GPU kernel as fallback when running on GPU\n",
            "WARNING:tensorflow:Layer gru will not use cuDNN kernel since it doesn't meet the cuDNN kernel criteria. It will use generic GPU kernel as fallback when running on GPU\n",
            "WARNING:tensorflow:Layer gru_1 will not use cuDNN kernel since it doesn't meet the cuDNN kernel criteria. It will use generic GPU kernel as fallback when running on GPU\n",
            "WARNING:tensorflow:Layer gru_1 will not use cuDNN kernel since it doesn't meet the cuDNN kernel criteria. It will use generic GPU kernel as fallback when running on GPU\n",
            "WARNING:tensorflow:Layer gru_1 will not use cuDNN kernel since it doesn't meet the cuDNN kernel criteria. It will use generic GPU kernel as fallback when running on GPU\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{'intent': 'weather', 'place': 'London'}"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "OKm0N9yAaosB",
        "outputId": "882f4bd4-cd0a-4383-c12a-2b1f8940baf2",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 156
        }
      },
      "source": [
        "from eywa.nlu import EntityExtractor\n",
        "\n",
        "x = ['what is the weather in tokyo', 'what is the weather', 'what is the weather like in kochi']\n",
        "y = [{'intent': 'weather', 'place': 'tokyo'}, {'intent': 'weather', 'place': 'here'}, {'intent': 'weather', 'place': 'kochi'}]\n",
        "\n",
        "ex = EntityExtractor()\n",
        "ex.fit(x, y)\n",
        "\n",
        "x_test = 'what is the weather in london like'\n",
        "print(ex.predict(x_test))\n"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "WARNING:tensorflow:Layer gru_2 will not use cuDNN kernel since it doesn't meet the cuDNN kernel criteria. It will use generic GPU kernel as fallback when running on GPU\n",
            "WARNING:tensorflow:Layer gru_2 will not use cuDNN kernel since it doesn't meet the cuDNN kernel criteria. It will use generic GPU kernel as fallback when running on GPU\n",
            "WARNING:tensorflow:Layer gru_2 will not use cuDNN kernel since it doesn't meet the cuDNN kernel criteria. It will use generic GPU kernel as fallback when running on GPU\n",
            "WARNING:tensorflow:Layer gru_3 will not use cuDNN kernel since it doesn't meet the cuDNN kernel criteria. It will use generic GPU kernel as fallback when running on GPU\n",
            "WARNING:tensorflow:Layer gru_3 will not use cuDNN kernel since it doesn't meet the cuDNN kernel criteria. It will use generic GPU kernel as fallback when running on GPU\n",
            "WARNING:tensorflow:Layer gru_3 will not use cuDNN kernel since it doesn't meet the cuDNN kernel criteria. It will use generic GPU kernel as fallback when running on GPU\n",
            "{'intent': 'weather', 'place': 'like'}\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hMd46fqHKrKh",
        "outputId": "6c240115-190e-4271-e063-fa561361a548",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        }
      },
      "source": [
        "from pyowm import OWM\n",
        "import logging\n",
        "\n",
        "LOGGER = logging.getLogger('main')\n",
        "# put your API key here:\n",
        "mgr = OWM('API-key')  \n",
        "# in older versions you had to load OWM().weather_manager() instead\n",
        "\n",
        "def get_weather_forecast(place):\n",
        "    LOGGER.warning(place)\n",
        "    observation = mgr.weather_at_place(place)\n",
        "    return observation.get_weather().get_detailed_status()\n",
        "\n",
        "print(get_weather_forecast('London'))"
      ],
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "London\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "broken clouds\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "m4e8gz-aMwY2"
      },
      "source": [
        "X_GREETING = ['Hii', 'helllo', 'Howdy', 'hey there', 'hey', 'Hi']\n",
        "Y_GREETING = [{'greet': 'Hii'}, {'greet': 'helllo'}, {'greet': 'Howdy'},\n",
        "              {'greet': 'hey'}, {'greet': 'hey'}, {'greet': 'Hi'}]\n",
        "\n",
        "EX_GREETING = EntityExtractor()\n",
        "EX_GREETING.fit(X_GREETING, Y_GREETING)"
      ],
      "execution_count": 8,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RjAweeTDMyv7"
      },
      "source": [
        "X_DATETIME = ['what day is today', 'date today', 'what time is it now', 'time now']\n",
        "Y_DATETIME = [{'intent' : 'day', 'target': 'today'}, {'intent' : 'date', 'target': 'today'},\n",
        "              {'intent' : 'time', 'target': 'now'}, {'intent' : 'time', 'target': 'now'}]\n",
        "\n",
        "EX_DATETIME = EntityExtractor()\n",
        "EX_DATETIME.fit(X_DATETIME, Y_DATETIME)"
      ],
      "execution_count": 9,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "r9wyTjwyNDGE"
      },
      "source": [
        "import datetime\n",
        "\n",
        "_EXTRACTORS = {\n",
        "  'taxi': None,\n",
        "  'weather': EX_WEATHER,\n",
        "  'greetings': EX_GREETING,\n",
        "  'datetime': EX_DATETIME,\n",
        "  'music': None\n",
        "}\n",
        "\n",
        "def question_and_answer(u_query):\n",
        "    '''Answer a user query\n",
        "    '''\n",
        "    q_class = CLF.predict(u_query)\n",
        "    if _EXTRACTORS[q_class] is None:\n",
        "      return 'Sorry, you have to upgrade your software!'\n",
        "\n",
        "    q_entities = _EXTRACTORS[q_class].predict(u_query)\n",
        "    if q_class == 'greetings':\n",
        "      return q_entities.get('greet', 'hello')\n",
        "    \n",
        "    if q_class == 'weather':\n",
        "      place = q_entities.get('place', 'London').replace('_', ' ')\n",
        "      return 'The forecast for {} is {}'.format(\n",
        "          place,\n",
        "          get_weather_forecast(place)\n",
        "      )\n",
        "\n",
        "    if q_class == 'datetime':\n",
        "      return 'Today\\'s date is {}'.format(\n",
        "          datetime.datetime.today().strftime('%B %d, %Y')\n",
        "      )\n",
        "    \n",
        "    return 'I couldn\\'t understand what you said. I am sorry.'"
      ],
      "execution_count": 10,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FFK72xR7bZ6M",
        "outputId": "c1d51358-e25a-42ba-95c2-92f966a87b92",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        }
      },
      "source": [
        "while True:\n",
        "  query = input('\\nHow can I help you? ')\n",
        "  print(question_and_answer(query))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "germany\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "The forecast for germany is light rain\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mzzfkiINolVv"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}